package com.abl.domain.model;

import com.abl.common.util.ObjectKit;
import com.abl.common.util.Regular;
import com.abl.common.util.StringKit;
import com.abl.common.util.coll.MapKit;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 统一参数接收对象
 *
 * @param <T>接收对象泛型
 */
public class Param<T> extends JSONObject {

    public ArrayList<Object> getList(String key) {
        JSONArray key1 = getJSONArray(key);
        if (key1 == null) return new ArrayList<>();
        return new ArrayList<Object>(key1);
    }

    public <T> ArrayList<T> getList(String type, Class<T> c) {
        JSONArray key1 = getJSONArray(type);
        if (key1 == null) return new ArrayList<T>();
        return new ArrayList<>(key1.toJavaList(c));
    }

    public String getString(String type, String defaultValue) {
        String string = getString(type);
        if (string == null) {
            return defaultValue;
        }
        return string;
    }


    /**
     * TODO 解析为指定类型
     * 比如 接收到 {a:1,b:2,c:3,d:4} 四个属性此时通过 param.get('a') 这种 Key-Value 的形式 全部获取
     * <p>
     * 但是，通过 parseObj 会返回转换后的对象
     * 如果 <ANY>类型中；只存在 d e f 三个属性；
     * 那么转换后返回的对象中值结果为（ d=4; e=null; f=null; ）
     *
     * @return
     */
    public <T> T parseObj(Class<T> tClass) {
        return null;
    }

    //当前参数是否为分页参数
    public boolean isPage() {
        return (StringKit.isNotBlank(this.getString("size")) && StringKit.isNotBlank(this.getString("current")));
    }

    public int pageCurrent() {
        return this.getIntValue("current");
    }

    public int pageSize() {
        return this.getIntValue("size");
    }

    public JSONObject getJSONObject(List<String> keys) {
        return new JSONObject(MapKit.set(this, keys));
    }

    public JSONObject getJSONObject(String keysStr) {
        return new JSONObject(MapKit.set(this, keysStr));
    }


    /**
     * 验证不允许为空
     */
    public boolean checkNotNull(Collection<String> keys) {
        return checkNotNullMessage(keys);
    }

    public boolean checkNotNull(String... keys) {
        return checkNotNullMessage(Arrays.asList(keys));
    }

    private boolean checkNotNullMessage(Collection<String> keys) {
        keys.forEach(key -> {
            if (ObjectKit.isBlank(this.get(key))) {
                throw new AblError("操作失败：参数【{0}】错误，不能为空。", key);
            }
        });
        return true;
    }

    public boolean checkRegular(Regular regular, Collection<String> keys) {
        return checkRegularMessage(regular, keys, null);
    }

    public boolean checkRegular(Regular regular, String... keys) {
        return checkRegularMessage(regular, Arrays.asList(keys), null);
    }

    public boolean checkRegular(Regular regular, Collection<String> keys, String errorMessage) {
        return checkRegularMessage(regular, keys, errorMessage);
    }

    public boolean checkRegular(Regular regular, String errorMessage, String... keys) {
        return checkRegularMessage(regular, Arrays.asList(keys), errorMessage);
    }

    private boolean checkRegularMessage(Regular regular, Collection<String> keys, String errorMessage) {
        keys.forEach(key -> {
            if (!regular.check(this.get(key))) {
                if (StringKit.isNotBlank(errorMessage)) {
                    throw new AblError(errorMessage);
                } else {
                    throw new AblError("操作失败：{0}", regular.defaultErrorMessage());
                }
            }
        });
        return true;
    }

}
